﻿using System;
using System.ServiceModel;
using AccountServiceReference;

public partial class UserControl_RetrievePassword : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (IsPostBack) return;
        MultiView1.ActiveViewIndex = 0;
    }
    protected void lbtnRetrieve_Click(object sender, EventArgs e)
    {
        try
        {
            var client = new AccountServiceClient();
            var username = txtUsername.Text.Trim();
            var email = txtEmail.Text.Trim();
            var password = client.GetPassword(username, email);
            if (!string.IsNullOrEmpty(password))
            {
                MultiView1.ActiveViewIndex = 1;
                Session["OldPassword"] = password;
            }
            else
            {
                ltrmsg.Text = "Account information not found!";
            }
        }
        catch (FaultException<Account> faultException)
        {
            ltrmsg.Text = faultException.Message;
        }
        catch (Exception ex)
        {
            ltrmsg.Text = ex.Message;
        }
        
    }
    protected void lbtntochangepwpage_Click(object sender, EventArgs e)
    {
        MultiView1.ActiveViewIndex = 2;
    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        try
        {
            var client = new AccountServiceClient();
            var code = txtCode.Text.Trim();
            var newPassword = txtNewPassword.Text.Trim();
            var result = client.UpdatePasswordByCode(code, newPassword);
            if (result)
            {
                MultiView1.ActiveViewIndex = 3;
            }
            else
            {
                msg2.Text = "Code invalid!";
            }
        }
        catch (FaultException<Account> faultException)
        {
            msg2.Text = faultException.Message;
        }
        catch (Exception ex)
        {
            msg2.Text = ex.Message;
        }
    }
}